import { server } from '../config/server';
import { useToken } from '../utils/token';


export const word = {
    /*
     * 获取若干个待背诵单词
     */
    getAnyWord(count) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/word/getAnyWord`,
                method: 'GET',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                success: (res) => {
                    console.log('【 Response in request getAnyWord 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request getAnyWord 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    /*
     * 保存用户背诵进度
     */
    saveUserProgress(progress) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/word/saveUserProgress`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: {
                    progress
                },
                success: (res) => {
                    console.log('【 Response in request saveUserProgress 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request saveUserProgress 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    // 获取背单词进度
    getUserProgress() {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/word/getUserProgress`,
                method: 'GET',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                success: (res) => {
                    console.log('【 Response in request getUserProgress 】', res);
                    // res.data.data = res.data.data.map(item => {
                    //     return {
                    //         ...item,              // 使用扩展运算符保留原有字段
                    //         time: item.value * 2  // 将value字段乘以2
                    //     };
                    // });
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request getUserProgress 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    // 获取对应id列表的单词
    loadThisWord(wordId) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/word/loadThisWord`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: { wordId },
                success: (res) => {
                    console.log('【 Response in request loadThisWord 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request loadThisWord 】', err);
                    reject({ success: false, err });
                }
            });
        });
    }
};

